package com.fingard.dsp.bank.directbank.icbc02.bankUtil;

import org.apache.axis.encoding.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.Key;

public class TripleDESHelper {
	private final static String s_p_key = "asdfghjklqwertyuiopzxcvb";
	private final static String s_p_iv = "00000000";
	 /**
     * 3DES加密，DESede/CBC/PKCS5Padding
     * @param p_key 24位密钥
     * @param p_iv 8位向量
     * @param p_plainData 明文字节
     * @return 密文字节
     * @throws Exception
     */
	private static byte[] encryptCBC(byte[] p_key, byte[] p_iv, byte[] p_plainData) throws Exception {
        Key deskey = null;
        DESedeKeySpec spec = new DESedeKeySpec(p_key);
        SecretKeyFactory keyFac = SecretKeyFactory.getInstance("DESede");
        deskey = keyFac.generateSecret(spec);
 
        Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
        IvParameterSpec ivps = new IvParameterSpec(p_iv);
        cipher.init(Cipher.ENCRYPT_MODE, deskey, ivps);
        byte[] cipherBytes = cipher.doFinal(p_plainData);
        return cipherBytes;
    }
 
	
	/**
     * 3DES解密，DESede/CBC/PKCS5Padding
     * @param p_key 24位密钥
     * @param p_iv 8位向量
     * @param p_cipherData 密文字节
     * @return 明文字节
     * @throws Exception
     */
    private static byte[] decryptCBC(byte[] p_key, byte[] p_iv, byte[] p_cipherData) throws Exception {
        Key deskey = null;
        DESedeKeySpec spec = new DESedeKeySpec(p_key);
        SecretKeyFactory keyFac = SecretKeyFactory.getInstance("DESede");
        deskey = keyFac.generateSecret(spec);
 
        Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
        IvParameterSpec ivps = new IvParameterSpec(p_iv);
        cipher.init(Cipher.DECRYPT_MODE, deskey, ivps);
 
        byte[] plainBytes = cipher.doFinal(p_cipherData);
        return plainBytes;
    }
    
    
    /**
     * 3DES加密，DESede/CBC/PKCS5Padding
     * @param p_key 24位密钥
     * @param p_iv 8位向量
     * @param p_plainData 明文字节
     * @return 密文字节
     * @throws Exception
     */
	public static String encryptCBC(String in_plainData) throws Exception {
		byte[] p_plainData = in_plainData.getBytes("utf-8");
        byte[] p_key = get3DesKey(s_p_key);
        byte[] p_iv = s_p_iv.getBytes();
        byte[] cipherBytes =encryptCBC(p_key,p_iv,p_plainData);
        String result = Base64.encode(cipherBytes);
        if(result != null){
            result = result.replaceAll("\n", "");
        }
        return result;
    }
 
	
	/**
     * 3DES解密，DESede/CBC/PKCS5Padding
     * @param p_key 24位密钥
     * @param p_iv 8位向量
     * @param p_cipherData 密文字节
     * @return 明文字节
     * @throws Exception
     */
	public static String decryptCBC(String in_cipherData) throws Exception {
		byte[] p_cipherData = Base64.decode(in_cipherData);
		byte[] p_key = get3DesKey(s_p_key);
	    byte[] p_iv = s_p_iv.getBytes();
        byte[] plainBytes = decryptCBC(p_key,p_iv,p_cipherData);
        return  (new String(plainBytes,"utf-8"));
    }
    
    
    
    /** 
     * 生成24字节的3DES密钥。 
     * （不够24字节，则补0；超过24字节，则取前24字节。） 
     * @param key 密钥字符串 
     * @return 
     */  
    private static byte[] get3DesKey(String key) {  
        byte[] keyBytes = new byte[24];  
        if(key.getBytes().length > 24) {  
            for(int i = 0;i<24;i++) {  
                keyBytes[i] = key.getBytes()[i];  
            }  
        } else {  
            for(int i = 0;i<24;i++) {  
                if(i < key.getBytes().length) {  
                    keyBytes[i] = key.getBytes()[i];  
                } else {  
                    keyBytes[i] = 0x00;  
                }  
            }  
        }  
        return keyBytes;  
    }  

    public static void main(String[] args)
    {
        String szSrc = "100.00";
        System.out.println("加密前的字符串:" + szSrc);
        String encoded;
		try {
			encoded = encryptCBC(szSrc);
			System.out.println("加密后的字符串:" +encoded);
			String srcBytes = decryptCBC(encoded);
		    System.out.println("解密后的字符串:" + srcBytes);
		} catch (Exception e) {
			e.printStackTrace();
		}
    }

    /*
    public static void main(String[] args)
    {
        String szSrc = "100.00";
        System.out.println("加密前的字符串:" + szSrc);
        String encoded;
		try {
			encoded = encryptCBC(szSrc);
			System.out.println("加密后的字符串:" +encoded);
			String srcBytes = decryptCBC("MIBRoCh/4Y+sQG/yr0QYEQ==");
		    System.out.println("解密后的字符串:" + srcBytes);
		} catch (Exception e) {
			e.printStackTrace();
		}
    }*/
}
